#!/bin/bash
# This script creates symbolic link in JBoss deploy directory pointing to exploded EAR

mode=$1
sleepTime=$2

if [ -z $sleepTime ];then
    sleepTime=7
fi

mySleep()
{
    if [ $1 -gt 0 ];then
        echo "$2"
        sleep $sleepTime
    fi
}

CURR_DIR=`pwd`

# Switch to script's directory
    BASEDIR=$(dirname $0)
    #echo $BASEDIR
    cd $BASEDIR

. ./setupEnv.sh

    SYMLINK=$JBOSS_HOME/server/default/deploy/DataModelling.ear
    MYDIR=`pwd`
    EAR_EXPLODED_DIR=$MYDIR/ear/target/DataModelling

echo ""
# Removing previous existing symlink
if [ "$mode" = "" -o "$mode" = "remove" ]; then
    if [ -e $SYMLINK ];then
        echo "    Removing previous symlink"
        rm -f $SYMLINK
        mySleep $sleepTime "        Giving the server some time to detect this change"
    else
        echo "   INFO: Link doesn't exist"
    fi
fi


# Create symlink 
if [ "$mode" = "" -o "$mode" = "create" ]; then
    echo "    Creating new symlink"
    if [ -e $SYMLINK ];then
        echo "WARNING: Symbolic link already exists"
        ls -lisa $SYMLINK
    else
        ln -s $EAR_EXPLODED_DIR $SYMLINK
    fi
fi

mySleep $sleepTime "        Giving you some time to see the result"

# and return back
cd $CURR_DIR

echo ""

